package com.hnx.admin.config;

import com.hnx.admin.core.LogProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/*
项目名称： core
创 建 人: 倔强的头发
创建日期： 2025/5/28   5:58:32 
描   述：
*/
@Slf4j
@EnableAsync
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(LogProperties.class)
public class AsyncConfig {
    private final LogProperties  logProperties;
    // 登记日志线程池（快速消费）
    @Bean("loginExecutor")
    public Executor registrationExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(logProperties.getLoginCorePoolSize());
        executor.setMaxPoolSize(logProperties.getLoginMaxPoolSize());
        executor.setQueueCapacity(logProperties.getLoginQueueCapacity());
        executor.setThreadNamePrefix("login-log-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); // 队列满时直接丢弃
        return executor;
    }
    // 操作日志线程池（可靠存储）
    @Bean("operationExecutor")
    public Executor operationExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(logProperties.getOperCorePoolSize());
        executor.setMaxPoolSize(logProperties.getOperMaxPoolSize());
        executor.setQueueCapacity(logProperties.getOperQueueCapacity());
        executor.setThreadNamePrefix("oper-log-");
        return executor;
    }

}
